import os

import cv2

out_putFile = './data/ywh'
if not os.path.exists(out_putFile):
    os.makedirs(out_putFile)

face_cascades = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
eye_cascades = cv2.CascadeClassifier('./cascades/haarcascade_eye.xml')

camera = cv2.VideoCapture(0)
count = 0

while cv2.waitKey(1) == -1:
    success, frame = camera.read()
    if success:
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        faces = face_cascades.detectMultiScale(gray, 1.3, 4, minSize=(120, 120))
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
            face_image = cv2.resize(gray[y:y + h, x:x + w], (200, 200))
            face_filename = '%s/%d.pgm' % (out_putFile, count)
            cv2.imwrite(face_filename, face_image)
            count += 1
        cv2.imshow("After detector", frame)
